<?php
// +----------------------------------------------------------------------
// | 日志标签管理
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: TagAction.class.php 18 2011-02-10 08:44:50Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class TagAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	//添加标签页面
	public function addTag() {
		$this->assign('action_link', array('href'=>__URL__.'/listTag/', 'text' => L('listTag')));
		$this->assign('ur_here',L('addTag'));
		$this->display();
	}
	
	public function insertTag() {
		if ($_REQUEST['act']) {	
			$tag = D('Tag');
			if (!$tag->create()) {
				$this->error($tag->getError());
			} else {
				if ($result = $tag->add()) {
					clear_cache_files();    // 清除缓存
					set_log('add(Tag, Id: '.$result.')');
					$this->assign('jumpUrl',__URL__.'/listTag/');
					$this->success(L('success'));
				} else {
					$this->error(L('error'));
				}
			}
		} else {
			$this->error(L('illegal'));
		}		
	}
	
	//标签列表
	public function listTag() {
		//排序字段 默认为主键
		if (isset($_REQUEST ['order'])) {
			$order = $_REQUEST ['order'];
		} else {
			$order = 'tag_hit';
		}
		//排序方式 默认按照倒序排列
		if (isset($_REQUEST ['sort'])) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}
		$tag  =  D('Tag');
		/*echo "<pre>";
		print_r($tag->getTagArray());
		echo "</pre>";*/
		//dump($tag->getTagArray());
		$list = $tag->getTagArray();
		$this->assign('action_link', array('href'=>__URL__.'/addTag/', 'text' => L('addTag')));
		$this->assign('ur_here',L('listTag'));
		$this->assign('list',$list);
		$this->assign('sort',$sort);
		$this->display();
	}
	
	//编辑标签
	public function editTag() {
		$tag = M('Tag');
		$vo	 = $tag->getByTagId($_REQUEST['id']);
		$this->assign('vo',$vo);
		$this->assign('action_link', array('href'=>__URL__.'/listTag/', 'text' => L('listTag')));
		$this->assign('ur_here',L('editTag'));
		$this->display();
	}
		
	//编辑链接
	public function updateTag() {
		$tag = D('Tag');
		if (!$tag->create()) {
			$this->error($tag->getError());
		} else {
			$result = $tag->save();
			if (false !== $result) {
				clear_cache_files();    // 清除缓存
				set_log('edit(Tag, Id: '.$tag->tag_id.')');
				$this->assign('jumpUrl',__URL__.'/listTag/');
				$this->success(L('success'));
			} else {
				$this->error(L('error'));
			}
		}
	}
	
	//删除标签
	public function dropTag() {
		$tag = M('Tag');
		$postsTag = M('PostsTag');
		$tag_id = intval($_REQUEST['id']);
		if ($tag->where("tag_id=$tag_id")->delete()) {		
			$count = $postsTag->where("tag_id=$tag_id")->delete();
			clear_cache_files();    // 清除缓存
			set_log('drop(Tag, Id: '.$tag_id.', PostsTag: '.$count.')');
			$this->assign('jumpUrl',__URL__.'/listTag/');
			$this->success(L('success'));
		} else {
			$this->error(L('error'));
		}
	}
	
}

?>